package comment

import (
	"context"
	"strconv"
	"strings"

	"mall/common/globalkey"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/api/internal/utils/parse"
	"mall/service/forum/rpc/forumclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetPostCommentListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 评论列表
func NewGetPostCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostCommentListLogic {
	return &GetPostCommentListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetPostCommentListLogic) GetPostCommentList(req *types.PostCommentListReq) (resp *types.PostCommentListResp, err error) {
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	var filters []string
	filters = append(filters, "ForumId,equalTo:"+strconv.FormatInt(l.svcCtx.Config.Query.PostForumId, 10))
	var preload string = "forumComment:Id,PostId,ParentId,UserId,NickName,UserAvatar,CommentStatus,CreatedAt;" +
		"forumCommentMeta:_upvote_count"
	var eagerLoad string = "forumComment:forumCommentMeta"
	if req.PostId != nil && *req.PostId > 0 { //查询文章下的评论
		filters = append(filters, "PostId,equalTo:"+strconv.FormatInt(int64(*req.PostId), 10))
		filters = append(filters, "Status,equalTo:"+strconv.FormatInt(int64(globalkey.StatusEnabled), 10))
	}

	var sorter *string
	if req.Sorter != nil {
		sorter = pointy.GetPointer(strings.TrimSpace(*req.Sorter))
	}
	var platformId uint32 = globalkey.ThirdPartyPlatformWechatMini
	res, err := l.svcCtx.ForumRpc.GetPostCommentList(l.ctx, &forumclient.PostCommentListReq{
		Page:       uint32(page),
		PageSize:   uint32(pageSize),
		Preload:    pointy.GetPointer(preload),
		EagerLoad:  pointy.GetPointer(eagerLoad),
		Filter:     pointy.GetPointer(strings.Join(filters, ";")),
		Sorter:     sorter,
		PlatformId: pointy.GetPointer(platformId),
		WithMeta:   req.WithMeta,
	})
	if err != nil {
		return nil, err
	}
	resp = &types.PostCommentListResp{}
	if len(res.List) > 0 {
		for _, v := range res.List {
			var meta []*types.MetaInfo
			if len(v.Meta) > 0 {
				for _, m := range v.Meta {
					if m.Key == nil || m.Value == nil || len(strings.TrimSpace(*m.Key)) == 0 || len(strings.TrimSpace(*m.Value)) == 0 {
						continue
					}
					key := strings.TrimSpace(*m.Key)
					val := strings.TrimSpace(*m.Value)
					meta = append(meta, &types.MetaInfo{
						Id:    m.Id,
						Key:   pointy.GetPointer(key),
						Value: pointy.GetPointer(val),
					})
				}

			}
			resp.List = append(resp.List, &types.PostCommentInfo{
				Id:             v.Id,
				ForumId:        v.ForumId,
				PostId:         v.PostId,
				ParentId:       v.ParentId,
				UserId:         v.UserId,
				NickName:       v.NickName,
				UserAvatar:     v.UserAvatar,
				Ip:             v.Ip,
				AnonymousUser:  v.AnonymousUser,
				CommentContent: v.CommentContent,
				CommentType:    v.CommentType,
				Status:         v.Status,
				CreatedAt:      v.CreatedAt,
				Meta:           meta,
			})
		}
	}
	return resp, nil
}
